Imports System.IO
Imports System.Data.SqlClient
Imports System
Imports System.Text
Imports System.Data.Odbc
Public Class FrmNeoadjuvant
    Public GV_ISEDIT As Boolean = False
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub LoadM_Status()
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("exec spGetlookup_Value 289", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("Code_desc", GetType(String)))
            list.Columns.Add(New DataColumn("Code", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("Code_desc")), "", objReader("Code_desc"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("Code")), "", objReader("Code"))
                i = i + 1
            Loop
            Me.cbxM_Status.DataSource = list
            Me.cbxM_Status.DisplayMember = "Code_desc"
            Me.cbxM_Status.ValueMember = "Code"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    Private Sub LoadN_Status()
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("exec spGetlookup_Value 283", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("Code_desc", GetType(String)))
            list.Columns.Add(New DataColumn("Code", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("Code_desc")), "", objReader("Code_desc"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("Code")), "", objReader("Code"))
                i = i + 1
            Loop
            Me.cbxN_Status.DataSource = list
            Me.cbxN_Status.DisplayMember = "Code_desc"
            Me.cbxN_Status.ValueMember = "Code"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    Private Sub LoadcT_Status()
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("exec spGetlookup_Value 274", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("Code_desc", GetType(String)))
            list.Columns.Add(New DataColumn("Code", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("Code_desc")), "", objReader("Code_desc"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("Code")), "", objReader("Code"))
                i = i + 1
            Loop
            Me.cbxcT_Status.DataSource = list
            Me.cbxcT_Status.DisplayMember = "Code_desc"
            Me.cbxcT_Status.ValueMember = "Code"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    Private Sub FrmNeoadjuvant_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Panel2.Enabled = False
        LoadcT_Status()
        LoadM_Status()
        LoadN_Status()
        LoadChemoAxillaryLN()
        LoadTypeBiopsy()
        LoadChemoResponse()
        LblClinLn.Text = FrmHistology.cbxClinAxillaryLN.Text
        LblExtChest.Text = FrmHistology.cbxExtChestWall.Text
        LblTumourSize.Text = FrmHistology.txtTumourSize.Text
        DOB.Value = "01/01/1900"
        If List_1stNeoadjuvant() = False Then
            btnEdit_Click(sender, e)
        Else
            BtnAdd_Click(sender, e)
        End If

        Panel1.Enabled = GV_FRMEVENT_ALLOW_EDIT
        If Approved() = True Then
            'btnAdd.Enabled = False
            'btnEdit.Enabled = False
            'btnDel.Enabled = False
            'Panel1.Enabled = False
            'Panel2.Enabled = False
            btnEdit.Enabled = False
            btnSave.Enabled = False
            BtnLock.Text = "Unlock"
            btnDel.Enabled = False
        End If
    End Sub
    Function Approved() As Boolean
        Dim vStatus As String = ""
        Dim vLockBy As String = ""
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("select * from NEOADJ where Neoadj_ID = '" & GV_SELECTED_NEOADJID & "'", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Do While objReader.Read
                vStatus = IIf(IsDBNull(objReader("Status")), "", objReader("Status"))
                vLockBy = IIf(IsDBNull(objReader("LockedBy")), "", objReader("LockedBy"))
            Loop

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
        If vLockBy <> "" Then
            Approved = True
        Else
            Approved = False
        End If
    End Function
    Function List_1stNeoadjuvant() As Boolean
        Dim vNeoadj As String
        vNeoadj = ""
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("select top 1 Neoadj_ID from Neoadj where status <> 'D' and Histo_ID  = '" & GV_SELECTED_HISTOLOGYID & "'", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Do While objReader.Read
                vNeoadj = IIf(IsDBNull(objReader("Neoadj_ID")), "", objReader("Neoadj_ID"))
            Loop

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
        If vNeoadj <> "" Then
            ListNeoAdjuvant(vNeoadj)
            GV_SELECTED_NEOADJID = vNeoadj
            List_1stNeoadjuvant = False
            Exit Function
        End If
        List_1stNeoadjuvant = True
    End Function
    Private Sub LoadChemoResponse()
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("exec spGetlookup_Value 269", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("Code_Desc", GetType(String)))
            list.Columns.Add(New DataColumn("Code", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("Code_Desc")), "", objReader("Code_Desc"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("Code")), "", objReader("Code"))
                i = i + 1
            Loop
            Me.cbxChemoResponse.DataSource = list
            Me.cbxChemoResponse.DisplayMember = "Code_Desc"
            Me.cbxChemoResponse.ValueMember = "Code"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    Private Sub LoadTypeBiopsy()
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("exec spGetlookup_Value 112", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("Code_desc", GetType(String)))
            list.Columns.Add(New DataColumn("Code", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("Code_desc")), "", objReader("Code_desc"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("Code")), "", objReader("Code"))
                i = i + 1
            Loop
            Me.cbxTypeBiopsy.DataSource = list
            Me.cbxTypeBiopsy.DisplayMember = "Code_desc"
            Me.cbxTypeBiopsy.ValueMember = "Code"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub
    Private Sub LoadChemoAxillaryLN()
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("exec spGetlookup_Value 264", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Dim i As Integer
            i = 1
            Dim list As New DataTable
            list.Columns.Add(New DataColumn("Code_Desc", GetType(String)))
            list.Columns.Add(New DataColumn("Code", GetType(String)))
            list.Rows.Add(list.NewRow())
            list.Rows(0)(0) = ""
            list.Rows(0)(1) = ""
            Do While objReader.Read
                list.Rows.Add(list.NewRow())
                list.Rows(i)(0) = IIf(IsDBNull(objReader("Code_Desc")), "", objReader("Code_Desc"))
                list.Rows(i)(1) = IIf(IsDBNull(objReader("Code")), "", objReader("Code"))
                i = i + 1
            Loop
            Me.cbxPostAxillaryLN.DataSource = list
            Me.cbxPostAxillaryLN.DisplayMember = "Code_Desc"
            Me.cbxPostAxillaryLN.ValueMember = "Code"

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        GV_ISEDIT = False
        Panel2.Enabled = True
        btnEdit.Visible = False
        btnDel.Visible = False
        BtnAdd.Visible = False
        btnSave.Visible = True
        btnCancel.Visible = True
        ClearText()
    End Sub
    Private Sub ClearText()
        DOB.Value = "01/01/1900"

        cbxcT_Status.SelectedIndex = -1
        cbxN_Status.SelectedIndex = -1
        cbxM_Status.SelectedIndex = -1
        txtTumourSize.Text = ""
        cbxTypeBiopsy.SelectedIndex = -1
        txtNotes.Text = ""
        TxtPostChemoTumourSize.Text = ""
        cbxPostAxillaryLN.SelectedIndex = -1
        cbxChemoResponse.SelectedIndex = -1

    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If GV_SELECTED_EVENTID <> "" Then
            GV_ISEDIT = True
            Panel2.Enabled = True
            btnEdit.Visible = False
            btnDel.Visible = False
            BtnAdd.Visible = False
            btnSave.Visible = True
            btnCancel.Visible = True
        End If
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        If GV_SELECTED_EVENTID <> "" Then
            Dim response As Integer

            response = MsgBox("Are you sure want to delete this record?", MsgBoxStyle.YesNo, "Delete")
            If response = 7 Then
                Exit Sub
            End If

            Dim myCommand As SqlCommand
            Dim SqlConn As New SqlConnection()
            SqlConn = New SqlConnection(GV_CONNETION)
            myCommand = New SqlCommand("spDelNeadjuvant", SqlConn)
            myCommand.CommandType = CommandType.StoredProcedure
            SqlConn.Open()

            myCommand.Parameters.Add("@Neoadj_ID", SqlDbType.VarChar)
            myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)
            myCommand.Parameters("@Neoadj_ID").Value = GV_SELECTED_NEOADJID
            myCommand.Parameters("@EnteredBy").Value = GV_USER
            myCommand.ExecuteNonQuery()

            myCommand.Cancel()
            SqlConn.Close()
            myCommand = Nothing
            SqlConn = Nothing
            Close()
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        SaveNoeAdjuvant()
        Panel2.Enabled = False
        Close()

    End Sub

    Private Sub SaveNoeAdjuvant()
        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("spInsertNoeAdjuvant", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()

        myCommand.Parameters.Add("@Neoadj_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Event_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Pt_id1", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Histo_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@t_stage_6", SqlDbType.VarChar)
        myCommand.Parameters.Add("@n_stage_6", SqlDbType.VarChar)
        myCommand.Parameters.Add("@m_stage_6", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Neoadj_tum_size", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Date_biopsy_done", SqlDbType.DateTime)
        myCommand.Parameters.Add("@Type_Biopsy", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Neoadj_Details", SqlDbType.VarChar)
        myCommand.Parameters.Add("@PostChemo_tum_size", SqlDbType.VarChar)
        myCommand.Parameters.Add("@PostChemo_Ax_LN", SqlDbType.VarChar)
        myCommand.Parameters.Add("@Response_chemo", SqlDbType.VarChar)
        myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)

        If GV_ISEDIT = True Then
            myCommand.Parameters("@Neoadj_ID").Value = GV_SELECTED_NEOADJID
        Else
            myCommand.Parameters("@Neoadj_ID").Value = "0"
        End If
        myCommand.Parameters("@Pt_id1").Value = GV_CUR_PATID
        myCommand.Parameters("@Event_ID").Value = GV_SELECTED_EVENTID
        myCommand.Parameters("@Histo_ID").Value = GV_SELECTED_HISTOLOGYID
        myCommand.Parameters("@t_stage_6").Value = cbxcT_Status.Text
        myCommand.Parameters("@n_stage_6").Value = cbxN_Status.Text
        myCommand.Parameters("@m_stage_6").Value = cbxM_Status.Text
        myCommand.Parameters("@Neoadj_tum_size").Value = Trim(txtTumourSize.Text)
        myCommand.Parameters("@Date_biopsy_done").Value = DOB.Value
        myCommand.Parameters("@Type_Biopsy").Value = Trim(cbxTypeBiopsy.Text)
        myCommand.Parameters("@Neoadj_Details").Value = Trim(txtNotes.Text)
        myCommand.Parameters("@PostChemo_tum_size").Value = Trim(TxtPostChemoTumourSize.Text)
        myCommand.Parameters("@PostChemo_Ax_LN").Value = IIf(cbxPostAxillaryLN.Text Is Nothing, "", cbxPostAxillaryLN.Text)
        myCommand.Parameters("@Response_chemo").Value = IIf(cbxChemoResponse.Text Is Nothing, "", cbxChemoResponse.Text)
        myCommand.Parameters("@EnteredBy").Value = GV_USER
        myCommand.ExecuteNonQuery()

        myCommand.Cancel()
        SqlConn.Close()
        myCommand = Nothing
        SqlConn = Nothing
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        If GV_ISEDIT = False Then ClearText() Else ListNeoAdjuvant(GV_SELECTED_NEOADJID)
        Panel2.Enabled = False
        BtnAdd.Visible = True
        btnEdit.Visible = True
        btnDel.Visible = True
        btnSave.Visible = False
        btnCancel.Visible = False
    End Sub
    Private Sub ListNeoAdjuvant(ByVal vId)
        Dim SqlConnection1 As New SqlConnection()
        SqlConnection1 = New SqlConnection(GV_CONNETION)
        If SqlConnection1.State = ConnectionState.Closed Then SqlConnection1.Open()
        Dim sqlCmd = New SqlCommand("select * from Neoadj where Neoadj_ID = '" & vId & "'", SqlConnection1)
        Try
            Dim objReader As SqlDataReader = sqlCmd.ExecuteReader
            Do While objReader.Read
                cbxcT_Status.Text = IIf(IsDBNull(objReader("t_stage_6")), -1, objReader("t_stage_6"))
                cbxN_Status.Text = IIf(IsDBNull(objReader("n_stage_6")), -1, objReader("n_stage_6"))
                cbxM_Status.Text = IIf(IsDBNull(objReader("m_stage_6")), -1, objReader("m_stage_6"))
                txtTumourSize.Text = IIf(IsDBNull(objReader("Neoadj_tum_size")), "", objReader("Neoadj_tum_size"))
                DOB.Value = IIf(IsDBNull(objReader("Date_biopsy_done")), "01/01/1900", objReader("Date_biopsy_done"))
                cbxTypeBiopsy.Text = IIf(IsDBNull(objReader("Type_Biopsy")), "", objReader("Type_Biopsy"))
                txtNotes.Text = IIf(IsDBNull(objReader("Neoadj_Details")), "", objReader("Neoadj_Details"))
                TxtPostChemoTumourSize.Text = IIf(IsDBNull(objReader("PostChemo_tum_size")), "", objReader("PostChemo_tum_size"))
                cbxPostAxillaryLN.Text = IIf(IsDBNull(objReader("PostChemo_Ax_LN")), -1, objReader("PostChemo_Ax_LN"))
                cbxChemoResponse.Text = IIf(IsDBNull(objReader("Response_chemo")), -1, objReader("Response_chemo"))
                Lbl_EnBy.Text = "Entered By:" & vbCrLf & IIf(IsDBNull(objReader("EnteredBy")), "", objReader("EnteredBy"))
                Lbl_UpdBy.Text = "Updated By:" & vbCrLf & IIf(IsDBNull(objReader("UpdatedBy")), "", objReader("UpdatedBy"))
                Lbl_LockBy.Text = "Locked By:" & vbCrLf & IIf(IsDBNull(objReader("LockedBy")), "", objReader("LockedBy"))

            Loop

        Catch e As Exception
            MsgBox(e.Source)
        End Try

        sqlCmd.Cancel()
        SqlConnection1.Close()
        sqlCmd = Nothing
        SqlConnection1.Close()
    End Sub

    Private Sub BtnLock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLock.Click
        If BtnLock.Text = "Lock" Then
            If GV_FRMEVENT_ALLOW_APPROVE = False Then
                MsgBox("Permission denied.", MsgBoxStyle.Information)
                Exit Sub
            End If
        End If

        If BtnLock.Text = "UnLock" Then
            If GV_FRMEVENT_ALLOW_APPROVE = False Then
                MsgBox("Permission denied.", MsgBoxStyle.Information)
                Exit Sub
            End If
        End If

        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("sp_LockUnlockNoeAdjuvant", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()
        myCommand.Parameters.Add("@Neoadj_ID", SqlDbType.VarChar)
        myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)
        myCommand.Parameters.Add("@LockFlag", SqlDbType.VarChar)

        myCommand.Parameters("@Neoadj_ID").Value = GV_SELECTED_NEOADJID
        myCommand.Parameters("@EnteredBy").Value = GV_USER
        myCommand.Parameters("@LockFlag").Value = Trim(BtnLock.Text)
        myCommand.ExecuteNonQuery()

        myCommand.Cancel()
        SqlConn.Close()
        myCommand = Nothing
        SqlConn = Nothing

        If BtnLock.Text = "Lock" Then
            BtnLock.Text = "UnLock"
            btnEdit.Enabled = False
            btnSave.Enabled = False
            btnDel.Enabled = False
            Lbl_LockBy.Text = "Locked By:" & vbCrLf & GV_USERNAME
        Else
            BtnLock.Text = "Lock"
            btnEdit.Enabled = GV_FRMEVENT_ALLOW_EDIT
            btnSave.Enabled = GV_FRMEVENT_ALLOW_EDIT
            btnDel.Enabled = GV_FRMEVENT_ALLOW_EDIT
            Lbl_LockBy.Text = "Locked By:"
        End If
    End Sub
End Class